import { defineStore } from 'pinia'

export interface LogoTextItem {
    id: string | number
    text: string
}

export interface LogoTextState {
    items: LogoTextItem[]
}

export const useLogoTextStore = defineStore('logoText', {
    state: (): LogoTextState => ({
        items: [],
    }),

    getters: {
        // 获取所有文本项目
        getAllItems: (state): LogoTextItem[] => state.items,

        // 根据ID查找文本项目
        getItemById: (state) => {
            return (id: LogoTextItem['id']): LogoTextItem | undefined =>
                state.items.find(item => item.id === id)
        }
    },

    actions: {
        // 添加文本项目
        add(item: LogoTextItem): void {
            this.items.push({ ...item })
        },

        // 根据ID删除文本项目
        remove(id: LogoTextItem['id']): void {
            this.items = this.items.filter(i => i.id !== id)
        },

        // 更新文本项目
        update(id: LogoTextItem['id'], text: string): void {
            const item = this.items.find(i => i.id === id)
            if (item) {
                item.text = text
            }
        },

        // 清空所有文本项目
        clear(): void {
            this.items = []
        }
    },

    persist: {
        key: 'chaye-logo-text',
        storage: localStorage,
    }
})
